import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class Main {
    static JFrame jf = new JFrame("OTTO计算器");
    static JPanel jp1 = new JPanel(new BorderLayout());
    static JPanel jp11 = new JPanel(new FlowLayout());
    static JPanel jp12 = new JPanel(new GridLayout(4, 4, 5, 5));
    static String Button_Num = "789/456*123-0C=+";
    static JTextArea jta = new JTextArea("");
    static List<Button> buttons = new ArrayList<>();


    public static void main(String[] args) {
        for (int i = 0; i < Button_Num.length(); ++i) {
            buttons.add(new Button(Button_Num.charAt(i)+""));
            jp12.add(buttons.get(i));
        }
        final Double[] temp1 = {0.0};
        final int[] flag = {-1};
        final boolean[] isequalDo = {false};
        for (int i =0;i<Button_Num.length();++i){

            if(Character.isDigit(buttons.get(i).getLabel().charAt(0)) ) {
                int finalI = i;
                buttons.get(i).addActionListener(e -> {
                    if(isequalDo[0]){
                        jta.setText("");
                        isequalDo[0] = false;
                    }
                    if(jta.getText().endsWith("+") || jta.getText().endsWith("-") || jta.getText().endsWith("/") || jta.getText().endsWith("*")) {
                        jta.setText("");
                    }

                    jta.append(buttons.get(finalI).getLabel());
                });


            }


            if(buttons.get(i).getLabel().equals("+")){
                int finalI1 = i;
                buttons.get(i).addActionListener(e -> {
                    temp1[0] = Double.parseDouble(jta.getText());
                    jta.append(buttons.get(finalI1).getLabel());
                    flag[0] = 1;
                });
            }
            if(buttons.get(i).getLabel().equals("-")){
                int finalI1 = i;
                buttons.get(i).addActionListener(e -> {
                    temp1[0] = Double.parseDouble(jta.getText());
                    jta.append(buttons.get(finalI1).getLabel());
                    flag[0] = 2;
                });
            }

            if(buttons.get(i).getLabel().equals("*")){
                int finalI1 = i;
                buttons.get(i).addActionListener(e -> {
                    temp1[0] = Double.parseDouble(jta.getText());
                    jta.append(buttons.get(finalI1).getLabel());
                    flag[0] = 3;
                });
            }

            if(buttons.get(i).getLabel().equals("/")){
                int finalI1 = i;
                buttons.get(i).addActionListener(e -> {
                    temp1[0] = Double.parseDouble(jta.getText());
                    jta.append(buttons.get(finalI1).getLabel());
                    flag[0] = 4;
                });
            }
            if(buttons.get(i).getLabel().equals("=")){
                buttons.get(i).addActionListener(e -> {
                    isequalDo[0] = true;
                    if(flag[0] == 1){
                        jta.setText(  temp1[0] + Double.parseDouble(jta.getText())+"");
                    } else if (flag[0] == 2) {
                        jta.setText(  temp1[0] - Double.parseDouble(jta.getText())+"");
                    } else if (flag[0] == 3) {
                        jta.setText(  temp1[0] * Double.parseDouble(jta.getText())+"");
                    }else{
                        jta.setText(  temp1[0] / Double.parseDouble(jta.getText())+"");
                    }
                });
            }
            if(buttons.get(i).getLabel().equals("C")){
                buttons.get(i).addActionListener(e -> jta.setText(""));
            }


        }







        jta.setPreferredSize(new Dimension(400,100));
        jp11.add(jta);
        jp1.add(jp11,BorderLayout.NORTH);
        jp1.add(jp12,BorderLayout.CENTER);
        jf.add(jp1);
        jf.setBounds(200, 200, 400, 400);
        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}